<?php
/*****************************************************************************
**	Файл:	Регистрация учетной записи пользователя							**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

if (SERVER_NO)
{
	if ($work->config['allow_registration'])
	{
		$template->array_data['ERROR_LOGIN'] = '';
		$template->array_data['ERROR_PASS'] = '';
		$template->array_data['ERROR_RE_PASS'] = '';
		$template->array_data['ERROR_EMAIL'] = '';
		$template->array_data['ERROR_NAME'] = '';
		$template->array_data['ERROR_FACTION'] = '';
		$template->array_data['ERROR_LANGUAGE'] = '';
		$template->array_data['ERROR_THEME'] = '';
		$template->array_data['ERROR_CAPTCHA'] = '';
		if ($work->check_post('login', true, true, REG_LOGIN) && $work->check_post('pass', true, true, REG_NAME) && $work->check_post('re_pass', true, true, REG_NAME) && $work->check_post('email', true, true, REG_EMAIL) && $work->check_post('name', true, true, REG_NAME) && $work->check_post('faction', true, true, '^[0-9]+\$', true) && $work->check_post('language', true, true, REG_LOGIN) && $work->check_post('theme', true, true, REG_LOGIN) && $work->check_post('captcha', true, true, '^[0-9]+\$', true) && $work->check_session('captcha', true, true, '^[0-9]+\$', true))
		{
			if ($_POST['pass'] == $_POST['re_pass'])
			{
				if ($_POST['captcha'] == $_SESSION['captcha'])
				{
					$db->select('id', DB_USER, TBL_USERS);
					$db->where('`login` = "' . $_POST['login'] . '"');
					$db->query();
					if (!$db->res_row())
					{
						$db->clean();
						$db->select('id', DB_USER, TBL_USERS);
						$db->where('`email` = "' . $_POST['email'] . '"');
						$db->query();
						if (!$db->res_row())
						{
							$regist['login'] = $_POST['login'];
							$regist['pass'] = md5(sha1($_POST['login'] . $_POST['pass']));
							$regist['name'] = $work->clean_field($_POST['name']);
							$regist['email'] = $_POST['email'];
							$regist['group'] = $work->config['group_def'];
							$regist['language'] = $_POST['language'];
							$regist['theme'] = $_POST['theme'];
							$regist['faction'] = $_POST['faction'];
							$regist['date_regist'] = date('Y-m-d H:i:s');
							$regist['last_ip'] = $_SERVER['REMOTE_ADDR'];
							$db->clean();
							$db->insert($regist, DB_USER, TBL_USERS);
							$db->query();
							$user_id = $db->insert_id;
							$db->clean();
							if ($user_id)
							{
								$_SESSION['user_login'] = $regist['login'];
								$mail_array = array();
								$activated_code = false;
								$_SESSION['info'] = true;
								if ($work->config['requires_activation'])
								{
									$activated_code = $user->gen_activated_code($user_id);
								}
								if ($activated_code)
								{
									$url_activated = $work->config['site_url'] . '?action=activate&login=' . $regist['login'] . '&email=' . $regist['email'];
									$mail_array['USER_NAME'] = $regist['name'];
									$mail_array['USER_EMAIL'] = $regist['email'];
									$mail_array['URL_ACCOUNT_ACTIVATE'] = $url_activated . '&activated_code=' . $activated_code;
									$mail_array['URL_ACTIVATE'] = $url_activated;
									$mail_array['USER_LOGIN'] = $regist['login'];
									$mail_array['USER_CODE_ACTIVATED'] = $activated_code;
									$mail_array['USER_PASS'] = $_POST['pass'];
									$mail_array['SERVER_URL'] = $work->config['site_url'];
									$mail_array['SERVER_NAME'] = $work->config['title'];
									$language->local_mail($regist['email'], $regist['login'], 'confirm_register', $mail_array);
									$_SESSION['info_txt'] = sprintf($language->main['send_activated_code'], $regist['name'], $work->filt_email($regist['email']));
								}
								else
								{
									$mail_array['USER_NAME'] = $regist['name'];
									$mail_array['USER_EMAIL'] = $regist['email'];
									$mail_array['USER_LOGIN'] = $regist['login'];
									$mail_array['USER_PASS'] = $_POST['pass'];
									$mail_array['SERVER_URL'] = $work->config['site_url'];
									$mail_array['SERVER_NAME'] = $work->config['title'];
									$language->local_mail($regist['email'], $regist['login'], 'register', $mail_array);
									if ($work->config['notify_admin_of_regist'])
									{
										$language->local_mail($work->config['admin_email'], 'Admin', 'notify_admin_new_regist', $mail_array);
									}
									$_SESSION['info_txt'] = sprintf($language->main['user_registered'], $regist['name']);
								}
								header('Location: ' . $work->config['site_url']);
								die('HACK!');
							}
							else
							{
								$_SESSION['error'] = true;
								$_SESSION['error_txt'] = $language->error['unknown'];
							}
						}
						else
						{
							$_SESSION['error'] = true;
							$_SESSION['error_txt'] = sprintf($language->error['email_already_used'], $work->filt_email($_POST['email']));
							$template->array_data['ERROR_EMAIL'] = ERROR_FIELD;
							unset($_POST['email']);
						}
					}
					else
					{
						$_SESSION['error'] = true;
						$_SESSION['error_txt'] = sprintf($language->error['login_already_used'], $_POST['login']);
						$template->array_data['ERROR_LOGIN'] = ERROR_FIELD;
						unset($_POST['login']);
					}
					$db->clean();
				}
				else
				{
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = $language->error['captcha'];
					$template->array_data['ERROR_CAPTCHA'] = ERROR_FIELD;
				}
			}
			else
			{
				$_SESSION['error'] = true;
				$_SESSION['error_txt'] = $language->error['pass_re_pass'];
				$template->array_data['ERROR_PASS'] = ERROR_FIELD;
				$template->array_data['ERROR_RE_PASS'] = ERROR_FIELD;
			}
		}
		if ($work->check_post('submit', true, true) && !$_SESSION['error'])
		{
			$_SESSION['error'] = true;
			$_SESSION['error_txt'] = $language->error['field_register_user'];
			if (!$work->check_post('login', true, true, REG_LOGIN)) $template->array_data['ERROR_LOGIN'] = ERROR_FIELD;
			if (!$work->check_post('pass', true, true, REG_NAME)) $template->array_data['ERROR_PASS'] = ERROR_FIELD;
			if (!$work->check_post('re_pass', true, true, REG_NAME)) $template->array_data['ERROR_RE_PASS'] = ERROR_FIELD;
			if (!$work->check_post('email', true, true, REG_EMAIL)) $template->array_data['ERROR_EMAIL'] = ERROR_FIELD;
			if (!$work->check_post('name', true, true, REG_NAME)) $template->array_data['ERROR_NAME'] = ERROR_FIELD;
			if (!$work->check_post('faction', true, true, '^[0-9]+\$', true)) $template->array_data['ERROR_FACTION'] = ERROR_FIELD;
			if (!$work->check_post('language', true, true, REG_LOGIN)) $template->array_data['ERROR_LANGUAGE'] = ERROR_FIELD;
			if (!$work->check_post('theme', true, true, REG_LOGIN)) $template->array_data['ERROR_THEME'] = ERROR_FIELD;
			if (!$work->check_post('captcha', true, true, '^[0-9]+\$', true)) $template->array_data['ERROR_CAPTCHA'] = ERROR_FIELD;
			if (!$work->check_session('captcha', true, true, '^[0-9]+\$', true)) $_SESSION['error_txt'] .= $language->main['user_captcha'] . ' (Cookie)';
		}
		unset($_SESSION['captcha']);
		$captcha = $work->gen_captcha();
		$_SESSION['captcha'] = $captcha['answer'];
		$template->template_file = 'register.html';
		$title .= ' - ' . $language->title['register'];
		$template->array_data['L_SERVER_NAME'] = $work->config['description'];
		$template->array_data['L_INPUT_REGISTER'] = $language->main['input_register'];
		$template->array_data['L_LOGIN'] = $language->main['user_login'];
		$template->array_data['L_PASS'] = $language->main['user_pass'];
		$template->array_data['L_RE_PASS'] = $language->main['user_re_pass'];
		$template->array_data['L_EMAIL'] = $language->main['user_email'];
		$template->array_data['L_NAME'] = $language->main['user_name'];
		$template->array_data['L_FACTION'] = $language->main['user_faction'];
		$template->array_data['L_LANGUAGE'] = $language->main['user_language'];
		$template->array_data['L_THEME'] = $language->main['user_theme'];
		$template->array_data['L_CAPTCHA'] = $language->main['user_captcha'];
		$template->array_data['L_REGISTER_USER'] = $language->main['register_login'];
		$template->array_data['U_REGISTER'] = $work->config['site_url'] . '?action=register';
		$template->array_data['D_LOGIN'] = '';
		if ($work->check_post('login', true, true, REG_LOGIN)) $template->array_data['D_LOGIN'] = $_POST['login'];
		$template->array_data['D_EMAIL'] = '';
		if ($work->check_post('email', true, true, REG_EMAIL)) $template->array_data['D_EMAIL'] = $_POST['email'];
		$template->array_data['D_NAME'] = '';
		if ($work->check_post('name', true, true, REG_NAME)) $template->array_data['D_NAME'] = $_POST['name'];
		$template->array_data['D_CAPTCHA_QUESTION'] = $captcha['question'];
		$factions = $work->get_factions();
		foreach ($factions as $key=>$value)
		{
			$template->array_data['ARRAY_FACTION'][$key]['ID_FACTION'] = $value['id'];
			$template->array_data['ARRAY_FACTION'][$key]['D_FACTION_NAME'] = $value['name'];
			$template->array_data['ARRAY_FACTION'][$key]['D_SELECTD'] = '';
			if ($work->check_post('faction', true, true, '^[0-9]+\$', true))
			{
				if ($_POST['faction'] == $value['id']) $template->array_data['ARRAY_FACTION'][$key]['D_SELECTD'] = ' selected="selected"';
			}
		}
		$languages = $work->get_languages();
		foreach ($languages as $key=>$value)
		{
			$template->array_data['ARRAY_LANGUAGE'][$key]['ID_LANGUAGE'] = $value['value'];
			$template->array_data['ARRAY_LANGUAGE'][$key]['D_LANGUAGE_NAME'] = $value['name'];
			$template->array_data['ARRAY_LANGUAGE'][$key]['D_SELECTD'] = '';
			if ($work->check_post('language', true, true, REG_LOGIN))
			{
				if ($_POST['language'] == $value['value']) $template->array_data['ARRAY_LANGUAGE'][$key]['D_SELECTD'] = ' selected="selected"';
			}
		}
		$themes = $work->get_themes();
		foreach ($themes as $key=>$value)
		{
			$template->array_data['ARRAY_THEME'][$key]['ID_THEME'] = $value;
			$template->array_data['ARRAY_THEME'][$key]['D_THEME_NAME'] = $value;
			$template->array_data['ARRAY_THEME'][$key]['D_SELECTD'] = '';
			if ($work->check_post('theme', true, true, REG_LOGIN))
			{
				if ($_POST['theme'] == $value) $template->array_data['ARRAY_THEME'][$key]['D_SELECTD'] = ' selected="selected"';
			}
		}
	}
	else
	{
		$_SESSION['error'] = true;
		$_SESSION['error_txt'] = $language->error['server_not_regist'];
		header('Location: ' . $work->config['site_url']);
		die('HACK!');
	}
}
else
{
	$_SESSION['error'] = true;
	$_SESSION['error_txt'] = $language->error['no_out_server'];
	$user->last_active();
	header('Location: ' . $work->config['site_url']);
	die('HACK!');
}
?>
